package com.vancouver.knows.ui.activity.login;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.ui.common.MyAutoCompleteTextView;
import com.vancouver.knows.util.PhoConstants;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.task.mark.ForgotPasswordTaskMark;

public class ForgetPasswordActivity extends BaseActivity implements IResultReceiver{

	private Context mContext;
	@Bind(R.id.common_tips)
	TextView  mCommonTips;
	@Bind(R.id.common_back)
	ImageView mCommonBack;
	@Bind(R.id.code)
	TextView mAreaCode;
	@Bind(R.id.area)
	TextView mArea;
	@Bind(R.id.get_code)
	TextView mGetCode;
	@Bind(R.id.phone_or_email)
	MyAutoCompleteTextView mAccount;
	private LoadingDialog loadingDialog;
	private String mSendAccount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_forget_psd_view);
		mContext = ForgetPasswordActivity.this;
		setViews();
		setListeners();
	}

	private void setViews() {
		loadingDialog = new LoadingDialog(mContext);
		registerForgetPsdBroastcast();
		Intent intent = getIntent();
		String tips = intent.getStringExtra("COMMON_TIPS");
		mCommonTips.setText(tips);
	}
	
	private void setListeners(){
		mCommonBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		mArea.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, ChooseCountryActivity.class);
				intent.putExtra("COMMON_TIPS", mContext.getString(R.string.choose_country).toString());
				intent.putExtra("CURRENT_OPEN_ACTIVITY", PhoConstants.CURRENT_OPEN_FROM_FORGET_PSD);
				mContext.startActivity(intent);
			}
		});
	}
	
	@OnClick(R.id.get_code)
	public void onGetCodeClick() {
		if (mAccount.getText().toString().trim().equals("")){
			Toast.makeText(mContext, mContext.getText(R.string.login_Tips), Toast.LENGTH_SHORT).show();
			return;
		}
		
		String phoneStr = "";
		String emailStr = "";
		BusinessModule bModule = imContext.getBusinessModule();
		ForgotPasswordTaskMark taskMark = bModule.getTaskMarkPool().getForgotPasswordTaskMarkList();
		//判断输入的是手机号验证还是邮箱验证
		if (mAccount.getText().toString().trim().contains("@") 
				&& mAccount.getText().toString().trim().contains(".")) {
			emailStr = mAccount.getText().toString().trim();
			mSendAccount = emailStr;
			bModule.getServiceWrapper().forgotPassword(ForgetPasswordActivity.this, taskMark, "", emailStr);
		}else{
			phoneStr = mAccount.getText().toString().trim();
			String code = getAreaCode();
			mSendAccount = code + phoneStr;
			bModule.getServiceWrapper().forgotPassword(ForgetPasswordActivity.this, taskMark, code + phoneStr, "");
		}
		
		if (loadingDialog != null)
			loadingDialog.showLoadingDialog(mContext.getString(R.string.send_code_loading));
	}
	
	private String getAreaCode(){
		String code = mAreaCode.getText().toString();
		while (code.contains("0")) {
			int begin = code.indexOf("0");
			if (begin != -1)
				code = code.substring(begin+1);
		}
		
		return code;
	}
	
	private void registerForgetPsdBroastcast(){
		IntentFilter intentFilter = new IntentFilter(); 
		intentFilter.addAction(PhoConstants.CHOOSE_COUNTRY_INFO_FORGET_PSD);  
        this.registerReceiver(ForgetPsdReceiver, intentFilter);
	}
	
	BroadcastReceiver ForgetPsdReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(PhoConstants.CHOOSE_COUNTRY_INFO_FORGET_PSD)) {
				mAreaCode.setText(intent.getStringExtra("AREA_CODE"));	
				mArea.setText(intent.getStringExtra("COUNTRY"));	
			}
		}
	};
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadingDialog != null){
			loadingDialog.dismiss();
		}
		if (ForgetPsdReceiver != null) {
			unregisterReceiver(ForgetPsdReceiver);
		}
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		if (loadingDialog != null)
			loadingDialog.closeLoadingDialog();
		if (exception != null && exception.getHttpCode() != 4 && !exception.getExMessage().equals("")){
			Toast.makeText(mContext, exception.getExMessage(), Toast.LENGTH_SHORT).show();
		}else{
			finish();
			Intent intent = new Intent(mContext, ModifyPasswordActivity.class);
			intent.putExtra("COMMON_TIPS", mContext.getString(R.string.input_new_psd).toString());
			intent.putExtra("ACCOUNT", mSendAccount);
			mContext.startActivity(intent);
		}
	}
	
}